package com.wuxf.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.wuxf.entity.DicDataInfo;
import com.wuxf.entity.ReceivePhone;
import com.wuxf.entity.ResponseBodyEntity;
import com.wuxf.entity.custom.PwdCustom;
import com.wuxf.entity.custom.UserInfoCustom;
import com.wuxf.service.DicDataService;
import com.wuxf.service.PwdService;
import com.wuxf.service.ReceiveService;

@Controller
@RequestMapping("pwd")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PwdController {

	@Autowired
	private ReceiveService receiveService;
	@Autowired
	private DicDataService dicDataService;
	@Autowired
	private PwdService pwdService;
	/**
	 * 跳转到分发密码界面
	 * @param model
	 * @return
	 */
	@RequestMapping("goToDistribute")
	public String goToDistribute(Model model,HttpSession session) {
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom!=null) {
			model.addAttribute("userInfoCustom", JSONObject.toJSONString(userInfoCustom));
			model.addAttribute("userInfo", userInfoCustom);
		}else {
			return "login";
		}
		List<ReceivePhone> receivePhones = receiveService.findByUserId(userInfoCustom.getUserId());
		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		model.addAttribute("receivePhones", receivePhones);
		model.addAttribute("typeDicDatas", typeDicDatas);
		model.addAttribute("typeDicDataArr", JSONArray.toJSONString(typeDicDatas));
		
		return "pwd/distribute";
	}
	
	@RequestMapping("findByUserIdAndPage")
	public @ResponseBody ResponseBodyEntity findByUserIdAndPage(int page,int limit,int userId,HttpSession session) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		PageInfo<List<ReceivePhone>> receivePhones = receiveService.findByUserIdAndPage(userId,pageInfo);
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(receivePhones.getList());
		responseBodyEntity.setCount(receivePhones.getTotal());
		return responseBodyEntity;
	}
	
	
	@RequestMapping("goToReceive")
	public String goToReceive(Model model,HttpSession session) {
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom!=null) {
			model.addAttribute("userInfoCustom", JSONObject.toJSONString(userInfoCustom));
			model.addAttribute("userInfo", userInfoCustom);
		}else {
			return "login";
		}
		List<ReceivePhone> receivePhones = receiveService.findByUserId(userInfoCustom.getUserId());
		model.addAttribute("receivePhones", receivePhones);
		return "pwd/receiveList";
	}
	
	
	/**
	 * 生成密码
	 * @param model
	 * @return
	 */
	@RequestMapping("generatePwd")
	public @ResponseBody ResponseBodyEntity generatePwd(int userId,String ticketType,int receiveId) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		responseBodyEntity.setData(pwdService.generatePwd(userId, ticketType, receiveId));
		responseBodyEntity.setState("1");
		responseBodyEntity.setMsg("生成密码");
		return responseBodyEntity;
	}
	
	@RequestMapping("/goToPwdDetails")
	public String goToPwdDetails(Model model,HttpSession session) {
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom!=null) {
			model.addAttribute("userInfoCustom", JSONObject.toJSONString(userInfoCustom));
			model.addAttribute("userInfo", userInfoCustom);
		}else {
			return "login";
		}
		List<DicDataInfo> isUsedDicDatas = dicDataService.findByEnumTypeId("3");
		model.addAttribute("isUsedDicDatas",JSONArray.toJSONString(isUsedDicDatas));
		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		model.addAttribute("typeDicDatas", typeDicDatas);
		model.addAttribute("typeDicDataArr", JSONArray.toJSONString(typeDicDatas));
		return "pwd/pwdDetails";
	}
	
	@RequestMapping("/goToLeaderPwdDetails")
	public String goToLeaderPwdDetails(Model model,HttpSession session) {
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom!=null) {
			model.addAttribute("userInfoCustom", JSONObject.toJSONString(userInfoCustom));
			model.addAttribute("userInfo", userInfoCustom);
		}else {
			return "login";
		}
		List<DicDataInfo> isUsedDicDatas = dicDataService.findByEnumTypeId("3");
		model.addAttribute("isUsedDicDatas",isUsedDicDatas);
		model.addAttribute("isUsedDicDataArr",JSONArray.toJSONString(isUsedDicDatas));
		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		model.addAttribute("typeDicDatas", typeDicDatas);
		model.addAttribute("typeDicDataArr", JSONArray.toJSONString(typeDicDatas));
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		model.addAttribute("areaDicDatas", areaDicDatas);
		model.addAttribute("areaDicDatasArr", JSONArray.toJSONString(areaDicDatas));
		return "pwd/leaderPwdDetails";
	}
	
	
	
	@RequestMapping("findByMapAndPage")
	public @ResponseBody ResponseBodyEntity findByMapAndPage(HttpSession session,int page,int limit,String isUsed,String areaId,String pwdValue,String ticketType,String startDate,String endDate) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		HashMap paraMap = new HashMap();
		paraMap.put("ticketType",ticketType);
		paraMap.put("startDate", startDate);
		if(StringUtils.isNotBlank(endDate)) {
  			endDate = endDate+"3";
  		}
		paraMap.put("endDate", endDate);
		paraMap.put("pwdValue", pwdValue);
		paraMap.put("isUsed", isUsed);
		paraMap.put("areaId", areaId);
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		}
		if(userInfoCustom!=null) {
			if(!userInfoCustom.getRoleId().equals("0")) {
				paraMap.put("createUser", userInfoCustom.getUserId());
			}
			PageInfo<List<PwdCustom>> pwdCustoms = pwdService.findByMapAndPage(pageInfo,paraMap);
			responseBodyEntity.setCode(0);
			responseBodyEntity.setData(pwdCustoms.getList());
			responseBodyEntity.setCount(pwdCustoms.getTotal());
		}else {
			responseBodyEntity.setCode(1);
			responseBodyEntity.setMsg("登陆失效");
			return responseBodyEntity;
		}
		return responseBodyEntity;
	}
}
